package com.stevin.tool.parser;

import java.io.IOException;
import java.nio.file.*;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;

import static com.stevin.tool.utils.CodeUtils.*;

/**
 * @author mars
 * @date 2018/11/11
 */
public class ClassNameParser {

    private static Set<String> wordSet = new HashSet<>();

    public static void main(String[] args) throws IOException {
        //
        // String clazzFile = ""

        Path rootPath = Paths.get("/Users/mars/Desktop/SourceTree/public/Sentinel");
        Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS)
                .map(path -> path.toFile())
                .filter(file -> file.isFile() && file.canRead() && file.getAbsolutePath().endsWith(".java"))
                .flatMap(file -> {
                    try {
                        return Files.readAllLines(file.toPath()).stream();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                })
                .filter(Objects::nonNull)
                .filter(line -> isPublicClassLine(line) || isMethodLine(line))
                .map(line -> {
                    if (containSymbols(line)) {
                        return removeSymbols(line);
                    }
                    return line;
                })
                .flatMap(word -> parseClassName(word))
                .filter(word -> !(isAnnotation(word) || isNumeric(word) || isCollection(word)))
                .filter(word -> {
                    if (!word.isEmpty()) {
                        return true;
                    }
                    return false;
                })
                .forEach(word-> {
                    if (!wordSet.contains(word.toLowerCase())){
                        System.out.println(word);
                        wordSet.add(word.toLowerCase());
                    }
                });

    }

    public static Stream<String> parseClassName(String line) {
        String[] classWords = line.split(" ");
        return Stream.of(classWords)
                .map(word -> word.trim())
                .filter(word -> !(isNativeType(word) || isNativeWord(word)));
    }

}
